package com.example.bysj_sb.controller;

import com.example.bysj_sb.domain.GraduateProject;
import com.example.bysj_sb.service.GraduateProjectCacheService;
import com.example.bysj_sb.service.GraduateProjectService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
public class GraduateProjectController {
    //生成一个实例
    @Autowired
    private GraduateProjectService graduateProjectService;
    @Autowired
    private GraduateProjectCacheService graduateProjectCacheService;
    //添加
    @PostMapping("/graduateProject.ctl")
    //参数为一个对象
    public String add(@RequestBody GraduateProject graduateProject ) {
        graduateProject.getGraduateProjectStatus().setId(1);
        graduateProject.getGraduateProjectStatus().setDescription("已提交");
        boolean added = graduateProjectCacheService.update(graduateProject);
        if (added) {
            log.info("提交成功！");
            return "提交成功！";
        } else {
            log.info("提交失败！");
            return "提交失败！";
        }
    }
    //查找全部
    @GetMapping("/graduateProject.ctl")
    public List<GraduateProject> findALL(){
        return graduateProjectService.findAll();
    }
    //根据id查找
    @GetMapping("/graduateProject.ctl/{id}")
    public GraduateProject find(@PathVariable("id") int id){
        GraduateProject graduateProject = graduateProjectService.find(id);
        if(graduateProject!=null){
            return graduateProject;
        }else {
            throw new RuntimeException("未查询到 "+id+" !");
        }
    }
}
